﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class NGUI_Extensions
{
    public static void SetColor(this UIWidget widget, long color)
    {
        widget.color = CommonUtil.toColor((uint)(color & 0xffffffff));
    }

	// public static void SetColor(this UIWidget widget, long color)
	// {
	// 	widget.color = CommonUtil.toColor(color);
	// }

    public static void SetEffectColor(this UILabel uilable, uint color)
    {
        uilable.effectColor = CommonUtil.toColor(color);
    }

    public static void SetEffectColor(this UILabel uilable, int color)
    {
        uilable.effectColor = CommonUtil.toColor(color);
    }

    public static DG.Tweening.Tweener DOColor(this UIWidget widget, long color, float duration)
    {
        Color c = CommonUtil.toColor((uint)(color & 0xffffffff));
        DG.Tweening.Tweener t = DG.Tweening.DOTween.To(() => widget.color, x => widget.color = x, c, duration);
        return t;
    }
    
    public static DG.Tweening.Tweener DOAlpha(this UIRect widget, float alpha, float duration)
    {
        DG.Tweening.Tweener t = DG.Tweening.DOTween.To(() => widget.alpha, x => widget.alpha = x, alpha, duration);
        return t;
    }

    public static EventDelegate Add(this List<EventDelegate> delegates, EventDelegate.Callback a)
    {
        return Add(delegates, a, false);
    }

    public static EventDelegate Add(this List<EventDelegate> delegates, EventDelegate.Callback a, bool oneShot)
    {
        var e = new EventDelegate(a);
        e.oneShot = oneShot;
        delegates.Add(e);
        return e;
    }

    public static EventDelegate Add(this List<EventDelegate> delegates, LuaInterface.LuaFunction a)
    {
        return Add(delegates, a.ToDelegate<EventDelegate.Callback>(), false);
    }

    public static EventDelegate Add(this List<EventDelegate> delegates, LuaInterface.LuaFunction a, bool oneShot)
    {
        var e = new EventDelegate(a.ToDelegate<EventDelegate.Callback>());
        e.oneShot = oneShot;
        delegates.Add(e);
        return e;
    }

    static void WidgetFixedAspect(UIWidget widget, float texRatio, bool isScreenBg = true)
    {
        float uiRatio = (float)widget.width / widget.height;
        if (isScreenBg)
        {
            uiRatio = (float)Screen.width / Screen.height;
        }

        if (texRatio == uiRatio)
        {
            widget.keepAspectRatio = UIWidget.AspectRatioSource.Free;
        }
        else if (texRatio > uiRatio)
        {
            widget.keepAspectRatio = UIWidget.AspectRatioSource.BasedOnHeight;
        }
        else
        {
            widget.keepAspectRatio = UIWidget.AspectRatioSource.BasedOnWidth;
        }
    }

    public static void FixedAspect(this UITexture uiTex, bool isScreenBg = true)
    {
        Texture tex = uiTex.mainTexture;
        if (tex == null)
        {
            return;
        }
        float texRatio = (float)tex.width / tex.height;

        WidgetFixedAspect(uiTex, texRatio, true);
    }

    public static void FixedAspect(this UI2DSprite uiSprite, bool isScreenBg = true)
    {
        Sprite sprite = uiSprite.sprite2D;
        if (sprite == null)
        {
            return;
        }

        Rect rect = sprite.rect;
        float texRatio = rect.width / rect.height;

        WidgetFixedAspect(uiSprite, texRatio, true);
    }

//    public static void FixSafeArea(this UIRect widget, Vector4 margin)
//    {
//        widget.leftAnchor.relative = margin.x;
//        widget.bottomAnchor.relative = margin.y;
//        widget.rightAnchor.relative = margin.z;
//        widget.topAnchor.relative = margin.w;
//        widget.ResetAndUpdateAnchors();
//    }
}
